package it.jack;

import java.awt.BorderLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyVetoException;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import database.DB;
import java.util.Map;
import javax.swing.*;
import oggetti.Guadagno;


import oggetti.Utente;
import org.omg.PortableInterceptor.USER_EXCEPTION;

import pannelli.*;
import pannelli.main.PannelloClienti;
import pannelli.main.PannelloMagazzino;
import pannelli.main.PannelloRicerca;
import pannelli.main.PannelloRiparazioni;
import pannelli.main.PannelloUtenti;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PannelloPrincipale extends JPanel {

    private class DispatcherKeyListener implements KeyEventDispatcher {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_F5) {
                    pannelloRicerca = new PannelloRicerca(-1);
                    try {
                        pannelloRicerca.setSelected(true);
                        Main.getInstance().addInDesktop(pannelloRicerca);
                    } catch (PropertyVetoException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            return false;
        }
    }
    private static final long serialVersionUID = -2670821635791362389L;
    private JButton clienti;
    private JButton ricerca;
    private JButton utenti;
    private JButton logout;
    private JButton esci;
    private JButton riparazioni;
    private JButton magazzino;
    private static PannelloPrincipale istance;
    private PannelloRicerca pannelloRicerca;
    private int altezza = 35;
    private int larghezza = 170;
    private boolean admin;

    public static PannelloPrincipale getIstance() {
        return istance;
    }

    public void disableButtons() {
        if (admin) {
            clienti.setEnabled(false);
            utenti.setEnabled(false);
        }
        ricerca.setEnabled(false);
        logout.setEnabled(false);
        esci.setEnabled(false);
        riparazioni.setEnabled(false);
        magazzino.setEnabled(false);
    }
    private void addMenuBar(){
       JMenuBar bar = new JMenuBar();
       JMenu menu = new JMenu("Opzioni");
       JMenuItem guadagno = new JMenuItem("Guadagno mensile");
       guadagno.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                 Map<String,Double> guadagni = new DB().getGuadagni();
                 if(guadagni!=null){
                    new Guadagno(guadagni).setVisible(true);     
                }else{
                    JOptionPane.showMessageDialog(null, "Nessun guadagno memorizzato!");
                }

            }
        });
       bar.add(menu);
       menu.add(guadagno);
       bar.setVisible(true);
       Main.getInstance().setJMenuBar(bar);
    }
    public void removeBar(){
        if(Main.getInstance().getUser().getUsername().equals("admin"))
           Main.getInstance().getJMenuBar().setVisible(false);
    }
    public void activateButtons() {
        Main.getInstance().disableDesktopPane();
        if (admin) {
            clienti.setEnabled(true);
            utenti.setEnabled(true);
        }
        ricerca.setEnabled(true);
        logout.setEnabled(true);
        esci.setEnabled(true);
        riparazioni.setEnabled(true);
        magazzino.setEnabled(true);
    }

    public PannelloPrincipale(boolean admin) {
        super();
        this.istance = this;
        this.admin = admin;
        if (admin) {
            initGUI();
            addPlus();
            addMenuBar();
        } else {
            initGUI();
        }
        addListener();

        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new DispatcherKeyListener());
        pannelloRicerca = null;
        setBackground();
    }

    private void setBackground() {
        ImageIcon imageBack = new ImageIcon("images/logo.png");
        JLabel background = new JLabel(imageBack);
        int w = this.getWidth();
        int h = this.getHeight();
        background.setBounds(0, 0, (int) (w * 0.8), (int) (h * 0.8));
        add(background, new Integer(Integer.MIN_VALUE));
    }

    private void addPlus() {
        clienti = new JButton();
        add(clienti, new AnchorConstraint(295, 968, 295 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
        clienti.setText("Gestione clienti");
        utenti = new JButton();
        add(utenti, new AnchorConstraint(542, 968, 542 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
        utenti.setText("Gestione utenti");
        clienti.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                goClienti();
            }
        });
        utenti.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                PannelloUtenti u = new PannelloUtenti();
                u.setVisible(true);
                try {
                    u.setSelected(true);
                    Main.getInstance().addInDesktop(u);
                } catch (PropertyVetoException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void goLogin() {
        Main.getInstance().setSize(300, 300);
        Main.getInstance().add(new Login(), BorderLayout.CENTER);
        Main.getInstance().centra();
    }

    private void goRiparazioni() {
        PannelloRiparazioni r;
        try {
            r = new PannelloRiparazioni();
            r.setVisible(true);
            setEnabled(true);
            Main.getInstance().addInDesktop(r);
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void goClienti() {
        PannelloClienti c;
        try {
            c = new PannelloClienti();
            c.setVisible(true);
            setEnabled(true);
            Main.getInstance().addInDesktop(c);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    private void addListener() {
        logout.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int i = JOptionPane.showConfirmDialog(null, "Sei sicuro di voler effettuare il logout?", "Logout", JOptionPane.YES_NO_OPTION);
                if (i == JOptionPane.YES_OPTION) {
                    Main.getInstance().remove(PannelloPrincipale.getIstance());
                    removeBar();
                    goLogin();
                }
            }
        });
        esci.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int i = JOptionPane.showConfirmDialog(null, "Sei Sicuro di voler uscire?", "Uscita", JOptionPane.YES_NO_OPTION);
                if (i == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });

        riparazioni.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                goRiparazioni();
            }
        });

        magazzino.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                PannelloMagazzino p = new PannelloMagazzino();
                p.setVisible(true);
                try {
                    p.setSelected(true);
                    Main.getInstance().addInDesktop(p);
                } catch (PropertyVetoException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        ricerca.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                pannelloRicerca = new PannelloRicerca(-1);
                try {
                    pannelloRicerca.setSelected(true);
                    Main.getInstance().addInDesktop(pannelloRicerca);
                } catch (PropertyVetoException e1) {
                    e1.printStackTrace();
                }
            }
        });

    }

    private void initGUI() {
    	setSize(Toolkit.getDefaultToolkit().getScreenSize());

        AnchorLayout thisLayout = new AnchorLayout();
        this.setLayout(thisLayout);
        {
            logout = new JButton();
            add(logout, new AnchorConstraint(34, 968, 34 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            logout.setText("Logout");
            logout.setPreferredSize(new java.awt.Dimension(30, 25));
        }
        {
            esci = new JButton();
            add(esci, new AnchorConstraint(106, 968, 106 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            esci.setText("Esci");
            esci.setPreferredSize(new java.awt.Dimension(95, 27));
        }
        {
            riparazioni = new JButton();
            add(riparazioni, new AnchorConstraint(464, 968, 464 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            riparazioni.setText("Riparazioni");
            riparazioni.setPreferredSize(new java.awt.Dimension(95, 22));
        }
        {
            magazzino = new JButton();
            add(magazzino, new AnchorConstraint(379, 968, 379 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            magazzino.setText("Magazzino");
            magazzino.setPreferredSize(new java.awt.Dimension(95, 23));
        }
        {
            ricerca = new JButton();
            add(ricerca, new AnchorConstraint(634, 968, 634 + altezza, 968 - larghezza, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            ricerca.setText("Ricerca");
            ricerca.setPreferredSize(new java.awt.Dimension(54, 77));
        }

    }
}
